In [13]:
import plotly
print(plotly.__version__)            # version 1.9.x required
plotly.offline.init_notebook_mode() # run at the start of every notebook

plotly.offline.iplot({
    "data": [{
        "x": [1, 2, 3],
        "y": [4, 2, 5]
    }],
    "layout": {
        "title": "hello world"
    }
})
1.9.3
Drawing...
In [14]:
# (*) How you communicate with Plotly's servers
import plotly.plotly as py 


# (*) Useful tools, e.g., get_sublots(), embed()
import plotly.tools as tls

 # Every function in this module will communicate with an external plotly server
py.iplot({                      # use `py.iplot` inside the ipython notebook
    "data": [{
        "x": [1, 2, 3],
        "y": [4, 2, 5]
    }],
    "layout": {
        "title": "hello world"
    }
}, filename='hello world',      # name of the file as saved in your plotly account
   privacy='public')       
Out[14]:
In [15]:
# (1) Two lists of numbers
x1 = [1, 2, 3, 5, 6]
y1 = [1, 4.5, 7, 24, 38]

# (2) Make dictionary linking x and y coordinate lists to 'x' and 'y' keys
trace1 = dict(x=x1, y=y1)

# (3) Make list of 1 trace, to be sent to Plotly
data = [trace1]


py.plot(data, filename='s0_first_plot', auto_open=False)
Out[15]:
'https://plot.ly/~ChristieMyburgh/23'
In [16]:
# (@) Sent data to Plotly and show result in notebook 
py.iplot(data, filename='s0_first_plot')
Out[16]:
In [17]:
# (*) Graph objects to piece together plots
from plotly.graph_objs import Data, Layout, Figure

# (*) Import the Scatter graph object
from plotly.graph_objs import Scatter

# Make three lists of numbers
x = [1, 2, 3, 5, 6]
y1 = [1, 4.5, 7, 24, 38]
y2 = [1, 4, 9, 25, 36]

# (1.1) Make a 1st Scatter object
trace1 = Scatter(
    x=x,           # x-coordinates of trace
    y=y1,          # y-coordinates of trace
    mode='markers'   # scatter mode (more in UG section 1)
)

# (1.2) Make a 2nd Scatter object
trace2 = Scatter(
    x=x,           # same x-coordinates
    y=y2,          # different y-coordinates
    mode='lines'     # different scatter mode
) 

# (2) Make Data object 
data = Data([trace1, trace2])  # (!) Data is list-like, must use [ ]

# (3) Make Layout object (Layout is dict-like)
layout = Layout(title='Fig 0.3: Some Experiment')

# (4) Make Figure object (Figure is dict-like)
fig = Figure(data=data, layout=layout) 

# (*) Import graph objects XAxis and YAxis
from plotly.graph_objs import XAxis, YAxis

# (6.1) Make XAxis object, add title key
xaxis = XAxis(title='Some independent variable')

# (6.2) Make YAxis object, add title key
yaxis = YAxis(title='Some dependent variable')

# (7) Update 'layout' key in the Figure object
fig['layout'].update(
    xaxis1=xaxis,  # link XAxis object to 'xaxis1' (corresp. to first/only x-axis)
    yaxis1=yaxis   # similarly for 'yaxis1'
)


# (@) Send Figure object to Plotly and show plot in notebook
py.iplot(fig, filename='s0_second-plot') 
Out[17]:
In [18]:
# (*) To communicate with Plotly's server, sign in with credentials file
import plotly.plotly as py

# (*) Useful Python/Plotly tools
import plotly.tools as tls

# (*) Graph objects to piece together plots
from plotly.graph_objs import *

import numpy as np  # (*) numpy for math functions and arrays

# (*) Graph objects to piece together plots
from plotly.graph_objs import *

import numpy as np  # (*) numpy for math functions and arrays

stream_ids = tls.get_credentials_file()['stream_ids']

# Get stream id from stream id list 
stream_id = stream_ids[0]

print(stream_id)

# Make instance of stream id object 
stream = Stream(
    token=stream_id,  # (!) link stream id to 'token' key
    maxpoints=80      # (!) keep a max of 80 pts on screen
)

# Initialize trace of streaming plot by embedding the unique stream_id
trace1 = Scatter(
    x=[],
    y=[],
    mode='lines+markers',
    stream=stream         # (!) embed stream id, 1 per trace
)

data = Data([trace1])


# Add title to layout object
layout = Layout(title='Time Series')

# Make a figure object
fig = Figure(data=data, layout=layout)

# (@) Send fig to Plotly, initialize streaming plot, open new tab
unique_url = py.plot(fig, filename='s7_first-stream', auto_open=False)



# (@) Make instance of the Stream link object, 
#     with same stream id as Stream id object
s = py.Stream(stream_id)

# (@) Open the stream
s.open()

# (*) Import module keep track and format current time
import datetime
import time

i = 0    # a counter
k = 5    # some shape parameter
N = 200  # number of points to be plotted

# Delay start of stream by 5 sec (time to switch tabs)
time.sleep(5)

while i<N:
    i += 1   # add to counter

    # Current time on x-axis, random numbers on y-axis
    x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    y = (np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1))[0]

    # (-) Both x and y are numbers (i.e. not lists nor arrays)

    # (@) write to Plotly stream!
    s.write(dict(x=x, y=y))

    # (!) Write numbers to stream to append current data on plot,
    #     write lists to overwrite existing data on plot (more in 7.2).

    time.sleep(0.08)  # (!) plot a point every 80 ms, for smoother plotting

# (@) Close the stream when done plotting
s.close()

tls.embed('https://plot.ly/~ChristieMyburgh/28/time-series/')
Out[18]:
"\n# (*) To communicate with Plotly's server, sign in with credentials file\nimport plotly.plotly as py\n\n# (*) Useful Python/Plotly tools\nimport plotly.tools as tls\n\n# (*) Graph objects to piece together plots\nfrom plotly.graph_objs import *\n\nimport numpy as np  # (*) numpy for math functions and arrays\n\n# (*) Graph objects to piece together plots\nfrom plotly.graph_objs import *\n\nimport numpy as np  # (*) numpy for math functions and arrays\n\nstream_ids = tls.get_credentials_file()['stream_ids']\n\n# Get stream id from stream id list \nstream_id = stream_ids[0]\n\nprint(stream_id)\n\n# Make instance of stream id object \nstream = Stream(\n    token=stream_id,  # (!) link stream id to 'token' key\n    maxpoints=80      # (!) keep a max of 80 pts on screen\n)\n\n# Initialize trace of streaming plot by embedding the unique stream_id\ntrace1 = Scatter(\n    x=[],\n    y=[],\n    mode='lines+markers',\n    stream=stream         # (!) embed stream id, 1 per trace\n)\n\ndata = Data([trace1])\n\n\n# Add title to layout object\nlayout = Layout(title='Time Series')\n\n# Make a figure object\nfig = Figure(data=data, layout=layout)\n\n# (@) Send fig to Plotly, initialize streaming plot, open new tab\nunique_url = py.plot(fig, filename='s7_first-stream', auto_open=False)\n\n\n\n# (@) Make instance of the Stream link object, \n#     with same stream id as Stream id object\ns = py.Stream(stream_id)\n\n# (@) Open the stream\ns.open()\n\n# (*) Import module keep track and format current time\nimport datetime\nimport time\n\ni = 0    # a counter\nk = 5    # some shape parameter\nN = 200  # number of points to be plotted\n\n# Delay start of stream by 5 sec (time to switch tabs)\ntime.sleep(5)\n\nwhile i<N:\n    i += 1   # add to counter\n\n    # Current time on x-axis, random numbers on y-axis\n    x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')\n    y = (np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1))[0]\n\n    # (-) Both x and y are numbers (i.e. not lists nor arrays)\n\n    # (@) write to Plotly stream!\n    s.write(dict(x=x, y=y))\n\n    # (!) Write numbers to stream to append current data on plot,\n    #     write lists to overwrite existing data on plot (more in 7.2).\n\n    time.sleep(0.08)  # (!) plot a point every 80 ms, for smoother plotting\n\n# (@) Close the stream when done plotting\ns.close()\n\ntls.embed('https://plot.ly/~ChristieMyburgh/28/time-series/')\n"
In [ ]: